package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.RoleDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.util.CRUD;
import com.nc.delivery_project.server.domain.Role;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by miroshnik on 12/28/2014.
 */
public class RoleService extends BaseService<Role> {

    public RoleService() {
        super(new RoleDao());
    }

    public RoleService(Connection connection, boolean isAutoCloseConnection) {
        super(new RoleDao(), connection, isAutoCloseConnection);
    }

    public RoleService(boolean isAutoCloseConnection) {
        super(new RoleDao(), isAutoCloseConnection);
    }

    @Override
    public Role read(int id) {
        Role role = null;
        try {
            dao.setConnection(getConnection());
            role = dao.read(id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        return role;
    }

    @Override
    public List<Role> read() {
        List<Role> roles = null;
        try {
            dao.setConnection(getConnection());
            roles = dao.read();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return roles;
    }
}
